//
//  Team9WorkViewController.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/13.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9WorkViewController.h"
#import "JQAFNetworkTool.h"
#import "Team9WorkModel.h"
#import "Team9WorkCell.h"
#import "Team9ProjectModel.h"
#import "Team9ProjectCell.h"
#import <Masonry.h>
#import "Team9BaseView.h"
#import "Team9workVc.h"
#import "Team9DesignVC.h"

@interface Team9WorkViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)UITableView *designTableView;
@property(nonatomic, strong)NSArray *array;
@property(nonatomic, strong)NSArray *designArray;
@property(nonatomic, strong)UIScrollView *scrollView;

@property(nonatomic, strong)UIButton *leftButton;
@property(nonatomic, strong)UIButton *rightButton;
@property(nonatomic, strong) UIView * lineView;

@property(nonatomic, strong) Team9RefreshTool *tool2;
@property(nonatomic, strong) Team9EmptyTool *empty2;
@property(nonatomic, assign) BOOL selectRight;

@end

@implementation Team9WorkViewController
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark 通知中心方法
- (void)cityId:(NSNotification *)notification{
    if (notification.userInfo != nil) {
        self.cityId = notification.userInfo[@"uId"];
        [self refresh];
    } else {
        self.cityId = nil;
        [self refresh];
    }
}
- (void)changecolor{
    [super changecolor];
    if ([Team9EveningType shareEvening].eveningFailling) {
        self.lineView.backgroundColor = [UIColor greenColor];
        if (self.selectRight) {
            [self.leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.rightButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        } else {
            [self.leftButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
            [self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    } else{
        self.lineView.backgroundColor = [UIColor orangeColor];
        if (self.selectRight){
            [self.leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.rightButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        } else {
            [self.leftButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [self.rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([Team9EveningType shareEvening].eveningFailling) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.designTableView.backgroundColor = [UIColor blackColor];
    } else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.designTableView.backgroundColor = [UIColor whiteColor];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cityId:) name:@"sendData" object:nil];
    self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    [self createHeaderView];
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.scrollView addSubview:self.tableView];
    self.scrollView.contentSize = CGSizeMake(WIDTH * 2, HEGHT);
    self.tableView.contentInset = UIEdgeInsetsMake(30, 0, 20 + 44 + 49, 0);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 140.0f * WIDTH / 375 + 60;
    
    CGRect screen = [UIScreen mainScreen].bounds;
    screen.origin.x = WIDTH;
    self.designTableView = [[UITableView alloc] initWithFrame:screen style:UITableViewStylePlain];
    [self.scrollView addSubview:self.designTableView];
    self.designTableView.contentInset = UIEdgeInsetsMake(30, 0, 20 + 44 + 49, 0);
    self.designTableView.delegate = self;
    self.designTableView.dataSource = self;
    self.designTableView.rowHeight = 40 + 250 * WIDTH / 375;
    
    self.empty = [Team9EmptyTool toolWithScrollView:self.tableView];
    self.empty2 = [Team9EmptyTool toolWithScrollView:self.designTableView];
    self.tableView.tableFooterView = [UIView new];
    self.designTableView.tableFooterView = [UIView new];
//    [self createDate];
    [self refresh];

}
- (void)createHeaderView
{
    CGRect screen = [UIScreen mainScreen].bounds;
    screen.size.height = 30;
    
    Team9BaseView *header = [[Team9BaseView alloc] init];
    header.frame = screen;
    [self.view addSubview:header];
//#warning 颜色
    
    UIView *line = [[Team9BaseView alloc] initWithType:ViewTYpeLight];
    line.frame = CGRectMake(0, screen.size.height, WIDTH, 5);
    [self.view addSubview:line];
    
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.leftButton setTitle:@"找工长" forState:UIControlStateNormal];
    //    [self.leftButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [header addSubview:self.leftButton];
    [self.leftButton addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    self.leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightButton setTitle:@"看施工项目" forState:UIControlStateNormal];
    //    [self.rightButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [header addSubview:self.rightButton];
    self.rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.rightButton addTarget:self action:@selector(clickRight:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(header);
        make.right.equalTo(header.mas_centerX).offset(-5);
    }];
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(header);
        make.left.equalTo(header.mas_centerX).offset(5);
    }];
    
    self.lineView = [UIView new];
    [self.view addSubview:self.lineView];
    self.lineView.backgroundColor = [UIColor orangeColor];
    [self clickLeft:self.leftButton];
    
}
- (void)clickLeft:(UIButton *)button{
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.leftButton);
        make.height.equalTo(@1);
        [self.lineView.superview updateConstraintsIfNeeded];
    }];
    if ([Team9EveningType shareEvening].eveningFailling) {
        [self.leftButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        [self.leftButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self.rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    self.selectRight = NO;
}
- (void)clickRight:(UIButton *)button{
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.rightButton);
        make.height.equalTo(@1);
        [self.lineView.superview updateConstraintsIfNeeded];
    }];
    if ([Team9EveningType shareEvening].eveningFailling) {
        [self.rightButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [self.leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        [self.rightButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self.leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    [self.scrollView setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
    self.selectRight = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (_scrollView == scrollView) {
        CGRect frame = self.lineView.frame;
        CGFloat left = self.leftButton.frame.origin.x;
        CGFloat right = self.rightButton.frame.origin.x;
        CGFloat leftWidth = self.leftButton.frame.size.width;
        CGFloat rightWidth = self.rightButton.frame.size.width;
        CGFloat proportion = scrollView.contentOffset.x / WIDTH;
        frame.origin.x = left + (right - left) * proportion;
        frame.size.width = leftWidth + (rightWidth - leftWidth) * proportion;
        self.lineView.frame = frame;
    }
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
        if (self.scrollView.contentOffset.x == 0) {
            [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(self.leftButton);
                make.height.equalTo(@1);
            }];
            self.selectRight = NO;
        } else if (self.scrollView.contentOffset.x == WIDTH){
            [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(self.rightButton);
                make.height.equalTo(@1);
            }];
            self.selectRight = YES;
        }
    }
    [self changecolor];
}
- (void)setDesignArray:(NSArray *)designArray
{
    if (_designArray != designArray) {
        _designArray = designArray;
        [self.designTableView reloadData];
    }
}
- (void)setArray:(NSArray *)array{
    if (_array != array) {
        _array = array;
        [self.tableView reloadData];
    }
}
- (void)refresh{
    NSString *url = @"http://api.meilijia.com/app/advanced_pro_gz_list?zone=&page=1&sort=";
    if (self.cityId) {
        url = [NSString stringWithFormat:@"http://api.meilijia.com/app/advanced_pro_gz_list?zone=&page=1&zone=%@&sort=", self.cityId];
    }
    self.tool = [Team9RefreshTool toolWithScrollView:self.tableView url:url style:^NSArray *(id result) {
        NSArray *array = [Team9WorkModel baseModelByArr:result[@"data"][@"advanced_pro_gz_list"][@"list"]];
        return array;
    }refresh:^(NSArray *array) {
        self.array = array;
        [self refresh2];
    } empty:self.empty];
}
- (void)refresh2{
    NSString *url2 = @"http://api.meilijia.com/app/pro_gz_work_list?city_id=&page=1";
    if (self.cityId) {
        url2 = [NSString stringWithFormat:@"http://api.meilijia.com/app/pro_gz_work_list?city_id=%@&page=1", self.cityId];
    }
    self.tool2 = [Team9RefreshTool toolWithScrollView:self.designTableView url:url2 style:^NSArray *(id result) {
        NSArray *array = [Team9ProjectModel baseModelByArr:result[@"data"][@"pro_gz_work_list"][@"collections"]];
        return array;
    }refresh:^(NSArray *array) {
        self.designArray = array;
    } empty:self.empty2];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableView) {
        return self.array.count;
    }
    if (tableView == _designTableView) {
        return self.designArray.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableView) {
        static NSString *reuse = @"Team9WorkCellReuse";
        Team9WorkCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[Team9WorkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        Team9WorkModel *model = self.array[indexPath.row];
        cell.model = model;
        return cell;
    }
    if (tableView == _designTableView) {
        static NSString *reuse = @"Team9ProjectCellReuse";
        Team9ProjectCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[Team9ProjectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        Team9ProjectModel *model = self.designArray[indexPath.row];
        cell.model = model;
        return cell;
    }
    return nil;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableView) {
        Team9workVc *designerVc = [[Team9workVc alloc] init];
        Team9WorkModel *model = self.array[indexPath.row];
        designerVc.Id = model.user_id;
        designerVc.url = [NSString stringWithFormat:@"http://api.meilijia.com/app/user_home_page?user_id=%@", model.user_id];
        self.parentViewController.hidesBottomBarWhenPushed = YES;
        [self.parentViewController.navigationController pushViewController:designerVc animated:YES];
        self.parentViewController.hidesBottomBarWhenPushed = NO;
        //    http://api.meilijia.com/app/user_home_page?user_id=12323657
    }
    if (tableView == _designTableView) {
        
//        NSLog(@"%@", [self.designArray[indexPath.row] modelId]);
        Team9DesignVC *designVC = [Team9DesignVC new];
        designVC.url = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page2?col_id=%@&tab=photo", [self.designArray[indexPath.row] modelId]];
        self.parentViewController.hidesBottomBarWhenPushed = YES;
        [self.parentViewController.navigationController pushViewController:designVC animated:YES];
        self.parentViewController.hidesBottomBarWhenPushed = NO;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
